﻿using Microsoft.Win32;
using ProgrammersCalculator.Command;
using ProgrammersCalculator.Model;
using System;
using System.ComponentModel;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Input;
using System.Windows.Media;

namespace ProgrammersCalculator.ViewModel
{
    public class HashValidatorViewModel : INotifyPropertyChanged
    {
        private ICommand _PreviewDropCommand;
        private ICommand _checkBoxChangedCommand;
        public string SelectedFileName { get; set; }

        public ICommand CheckBoxChangedCommand
        {
            get { return _checkBoxChangedCommand ?? (_checkBoxChangedCommand = new RelayCommand(HandleRadioButton)); }
            set
            {
                _checkBoxChangedCommand = value;
                NotifyPropertyChanged("CheckBoxChangedCommand");
            }
        }

        private void HandleRadioButton(object obj)
        {

            HashAlgo = obj.ToString();
            Refresh();
        }
        private string _hashValue;
        private string _fileSize;
        private string _fileName;
        private string _compareWith;
        private SolidColorBrush _status;
        private string _hashType = "MD5";

        public string HashAlgo
        {
            get { return _hashType; }
            set
            {
                _hashType = value;
                NotifyPropertyChanged("HashAlgo");
            }
        }
        public SolidColorBrush Status
        {
            get { return _status; }
            set
            {
                _status = value;
                NotifyPropertyChanged("Status");
            }
        }
        public string CompareWith
        {
            get { return _compareWith; }
            set
            {
                _compareWith = value;

                NotifyPropertyChanged("CompareWith");
                CompareValue();
            }
        }

        private void CompareValue()
        {
            if (CompareWith.Trim().Equals(HashValue, StringComparison.CurrentCultureIgnoreCase))
                Status = new SolidColorBrush(Colors.Green);
            else
                Status = new SolidColorBrush(Colors.Red);
        }
        public string FileName
        {
            get { return _fileName; }
            set
            {
                _fileName = value;
                NotifyPropertyChanged("FileName");
            }
        }


        public ICommand PreviewDropCommand
        {
            get { return _PreviewDropCommand ?? (_PreviewDropCommand = new RelayCommand(HandlePreviewDrop)); }
            set
            {
                _PreviewDropCommand = value;
                NotifyPropertyChanged("PreviewDropCommand");
            }
        }

        public string FileSize
        {
            get { return _fileSize; }
            set
            {
                _fileSize = value;
                NotifyPropertyChanged("FileSize");
            }
        }

        public string HashValue
        {
            get { return _hashValue; }
            set
            {
                _hashValue = value;

                NotifyPropertyChanged("HashValue");
            }
        }
        #region The method encapsulated in the relay command
        private void HandlePreviewDrop(object inObject)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.DefaultExt = "*.*"; // Required file extension 
            fileDialog.Filter = "All files (*.*)|*.*"; // Optional file extensions


            bool? res = fileDialog.ShowDialog();
            if (res.HasValue && res.Value)
            {
                SelectedFileName = fileDialog.FileName;
                Refresh();
            }
        }

        private void Refresh()
        {
            StreamReader sr = new StreamReader(SelectedFileName);
            FileName = SelectedFileName;
            FileSize = new FileInfo(FileName).Length.ToString() + "Bytes";

            if (HashAlgo.Equals("MD5"))
                HashValue = ComputeHash(sr.BaseStream, "MD5");
            else if (HashAlgo.Equals("SHA1"))
                HashValue = HashValue = ComputeHash(sr.BaseStream, "sha1");
            else
                HashValue = HashValue = ComputeHash(sr.BaseStream, "sha256");
            sr.Close();
        }
        private string ComputeHash(Stream inputStream, string algoType)
        {
            var algo = HashAlgorithm.Create(algoType);
            return ByteArrayToString(algo.ComputeHash(inputStream));

        }
        public static string ByteArrayToString(byte[] ba)
        {
            string hex = BitConverter.ToString(ba);
            return hex.Replace("-", "");
        }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string name)
        {

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }


    }
}
